import 'package:app_router_demo/services/http_service.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:provider/provider.dart';
import 'config/app_router.dart';
import 'providers/auth_provider.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  // 模拟应用启动时的初始化
  await Future.delayed(const Duration(milliseconds: 500));
  await HttpService().initialize();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  final AuthProvider _authProvider = AuthProvider(); // 单例 AuthProvider
  late final AppRouter _appRouter;

  MyApp({super.key}) {
    _appRouter = AppRouter(authGuard: AuthGuard(authProvider: _authProvider));
  }

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider<AuthProvider>.value(value: _authProvider),
        // 你可能还有其他的 Provider，例如 HomeTabProvider 等
      ],
      child: MaterialApp.router(
        builder: FlutterSmartDialog.init(),
        debugShowCheckedModeBanner: false,
        title: 'Flutter AutoRoute Demo',
        theme: ThemeData(primarySwatch: Colors.blue),
        routerConfig: _appRouter.config(),
      ),
    );
  }
}
